
#ifndef ServerApp_h__
#define ServerApp_h__
#include "IObject/IServerObject.h"
#include "boost/thread/shared_mutex.hpp"
#include <map>

#include "SessionManager.h"
#include "ModuleManager.h"
#include "TimerManager.h"
#include "TCPClient.h"
#include "LoggerManager.h"

class ServerApp : public IServerObject
{
public:
	ServerApp();
	~ServerApp();

public:
	virtual void registerCmdHandler(int cmd, void* pKey, const MessageHandler & handler);
	virtual void unregisterCmdHandler(int cmd, void* pKey);
	virtual IPropertyParser* getPropertyParser();
	virtual ISessionManager* getSessionManager();
	virtual ITimerManager*   getTimerManager();
	virtual ITCPClient*		 getTCPClient();
	virtual ILoggerManager*	 getLoggerManager();

public:
	void Process(SocketID socketID,
		int cmd,
		const void * pBuffer, uint32_t size,
		const boost::function< void() > & fun_callback = boost::function< void() >()
		);

public:
	bool	start(const char * config_file, boost::asio::io_service * pIOService);
	void	stop();

private:
	void StartSessionManager();
	void LoadModules();

	void check_expired_socket(int expired_max);

private:

	struct Handler
	{
		void*			key;
		MessageHandler	handler;
	};

	typedef std::multimap<int, Handler> MessageHandlerMap;
	MessageHandlerMap m_MessageHandlerMap;

	typedef std::multimap<int, Handler>::iterator ITER;
	boost::shared_mutex	m_singals_lock;

	IPropertyParser		*m_pPropertyParser;
	ModuleManager		m_ModuleManager;
	SessionManager		m_SessonManager;
	TimerManager		*m_pTimerManager;
	TCPClient			m_TCPClient;
	TimerID				m_Timer_Check_Socket;

	LoggerManager		m_LoggerManager;
};

ServerApp& GetServerApp();

#endif // ServerApp_h__